package com.itic.demo.modelfiles.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.demo.modelfiles.persistence.model.ModelFiles;
import com.itic.demo.modelfiles.service.ModelFilesService;

/**
 * FileUploadController
 * <p>Company: itic</p>
 * @author: lmc
 * @date: 2015年5月8日 下午3:41:21
 * @version: V1.0
 */
@Controller
@RequestMapping(value = "/fileInfo")
public class FileInfoController extends BaseController{
	
    @Autowired
    private ModelFilesService filesService;
    
    @RequestMapping(value = "init",method=RequestMethod.GET)
    public String initMain(){
        return "/test/fileInfo";
    }
    
    /**
     * 文件上传 -文件信息保存到数据库
     * 
     * @param file
     * @param mTName
     * @return String
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String,Object> fileUploadWithDB(ModelFiles templet,
            HttpServletRequest request) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        String result = ResponseCode.SERVER_ERROR.getCode();
        
        //添加文件ID 和  业务ID
        String uuid = IdGen.uuid();
        templet.setId(uuid);
        //注意  把文件服务器保存的ID保存到  === 记得在前台保存
/*      if (StringHelper.isNotBlank(files.getId())){
        	templet.setBusinessId(files.getId());
        }*/
        try {
        	result = filesService.add(templet);
        	resultMap.put(IticConst.DATA, uuid);
        	resultMap.put(IticConst.RESULT, result);
        	return resultMap;
		} catch (Exception e) {
			resultMap.put("result", result);//返回文件上传结果
			return resultMap;
		}
    }
    
    /**
     * 删除服务器文件
     * @param id
     * @return
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, String> del(String id){
        Map<String,String> resultMap = new HashMap<String,String>();
        String result = ResponseCode.SERVER_ERROR.getCode();
        if( StringHelper.isNotBlank(id) ){
        	ModelFiles templet = new ModelFiles();
            templet.setId(id);
            result = filesService.delete(templet);
        }
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }

    
    /**
     * 根据文件Id、字段名称组装文件信息
     * @return
     */
    @RequestMapping(value = "/getFileInfos")
    @ResponseBody
    public Map<String, Object> getFileInfos(HttpServletRequest request,String fileIds,String field, Model model) {
        Map<String,Object> fileData = new HashMap<String,Object>();
        Map<String,Object> fileItem = new HashMap<String,Object>();
        if (StringHelper.isNotBlank(fileIds)){
            List<ModelFiles> files = filesService.getFiles(fileIds);
            List<Map<String,Object>> fileList = new ArrayList<Map<String,Object>>();
            for (ModelFiles files2 : files) {
                Map<String,Object> file = new HashMap<String,Object>();
                file.put("id", files2.getId());
                file.put("name", files2.getFileName());
                file.put("size", files2.getFileSize());
                file.put("path", files2.getFilePath());
                file.put("type", files2.getField());
                fileList.add(file);
            }
            fileItem.put(field, fileList);
            fileData.put(IticConst.DATA, fileItem);
            fileData.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        }
      return fileData;
    }
    /**
     * 根据文件Id、字段名称组装文件信息
     * @return
     */
    @RequestMapping(value = "/getFileInfo")
    @ResponseBody
    public Map<String, Object> getFileInfo(HttpServletRequest request,String fileInfoId,String field, Model model) {
    	Map<String,Object> fileData = new HashMap<String,Object>();
    	if (StringHelper.isNotBlank(fileInfoId)){
    		ModelFiles file = filesService.getFile(fileInfoId);
    		fileData.put(IticConst.DATA, file);
    		fileData.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
    	}
    	return fileData;
    }
}
